package com.JieCheng.service.impl;

import com.JieCheng.dao.model.Subject;
import com.JieCheng.dao.model.User;
import com.JieCheng.service.SubjectService;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Zhang on 2017/5/11.
 */
@Service
public class SubjectServiceImpl extends BaseServiceImpl implements SubjectService {

    @Override
    public String exportSubjectTemplet(HttpServletRequest httpServletRequest,HttpServletResponse response) throws Exception {
        User user=(User)httpServletRequest.getSession().getAttribute("user");
        if(user.getRoleId()!=4){
            return "权限不足";
        }
        String[] label={"题目内容","题目说明","题目类型(单选/多选/判断)","题目选项(以';'分隔)","答案","分值(仅数字)","科目(1/2/3/4)","车型"};
        excelUtil.excelTempletExport("题目模板",label,response);
        return "导出成功";
    }

    @Override
    public String importTemplet(MultipartFile file,HttpServletRequest httpServletRequest) throws IOException, BiffException {
        User user=(User)httpServletRequest.getSession().getAttribute("user");
        if(user.getRoleId()!=4){
            return "权限不足";
        }
        Subject []subjects=excelUtil.templetToSubjects(file);
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int num=0;
        for(int i=0;i<subjects.length;i++){
            if(!(subjects[i].getType().equals("单选")||subjects[i].getType().equals("多选")||subjects[i].getType().equals("判断"))){
                continue;
            }
            if(!(subjects[i].getCarexam().equals("1") || subjects[i].getCarexam().equals("2") ||
                 subjects[i].getCarexam().equals("3") || subjects[i].getCarexam().equals("4"))){
                continue;
            }
            subjectMapper.addSubject(subjects[i].getContent(),subjects[i].getInfo(),subjects[i].getType(),subjects[i].getSelect(),
                                     subjects[i].getAnswer(),subjects[i].getGrade(),subjects[i].getCarexam(),subjects[i].getCartype(),
                                     subjects[i].getUrl(),sdf.format(dt),user.getUserId());
            num++;
        }
        return "成功新增"+num+"条";
    }

    @Override
    public String addCollectSubject(HttpServletRequest httpServletRequest) {
        User user=(User)httpServletRequest.getSession().getAttribute("user");
        String subjectId=httpServletRequest.getParameter("subjectId");
        int userId=user.getUserId();
        boolean result=subjectMapper.addCollectSubject(userId,Integer.parseInt(subjectId));
        if (result){
            return"新增成功";
        }else{
            return "添加失败";
        }
    }

    @Override
    public String deleteCollectSubject(HttpServletRequest httpServletRequest) {
        User user=(User)httpServletRequest.getSession().getAttribute("user");
        String subjectId=httpServletRequest.getParameter("subjectId");
        int userId=user.getUserId();
        boolean result=subjectMapper.deleteCollectSubject(userId,Integer.parseInt(subjectId));
        if (result){
            return"删除成功";
        }else{
            return "删除失败";
        }
    }

    @Override
    public String deleteErrorSubject(HttpServletRequest httpServletRequest) {
        User user=(User)httpServletRequest.getSession().getAttribute("user");
        String subjectId=httpServletRequest.getParameter("subjectId");
        int userId=user.getUserId();
        boolean result=subjectMapper.deleteErrorSubject(userId,Integer.parseInt(subjectId));
        if (result){
            return"删除成功";
        }else{
            return "删除失败";
        }
    }

    @Override
    public boolean addErrorSubject(HttpServletRequest httpServletRequest) {
        User user=(User)httpServletRequest.getSession().getAttribute("user");
        String subjectId=httpServletRequest.getParameter("subjectId");
        boolean result=subjectMapper.addErrorSubject(user.getUserId(),Integer.parseInt(subjectId));
        return result;
    }

    @Override
    public String selectRandomSubject(HttpServletRequest httpServletRequest) {
        String carExam=httpServletRequest.getParameter("carexam");
        String carType=httpServletRequest.getParameter("cartype");
        Subject subject;
        switch (carExam){
            case "科目一":carExam="1"; break;
            case "科目二":carExam="2"; break;
            case "科目三":carExam="3"; break;
            case "科目四":carExam="4"; break;
            default:carExam="0"; break;
        }
        do{
            subject=subjectMapper.selectRandomSubject(carExam,carType);
        }while (subject==null);
        return jsonLittleData.ObjectToJson(subject);
    }

    @Override
    public String centerPage(HttpServletRequest httpServletRequest, Model model, String examtype, String carexam, String cartype) {
        User user=(User)httpServletRequest.getSession().getAttribute("user");
        model.addAttribute("user", user);
        switch (carexam){
            case "1":model.addAttribute("carexam", "科目一");break;
            case "2":model.addAttribute("carexam", "科目二");break;
            case "3":model.addAttribute("carexam", "科目三");break;
            case "4":model.addAttribute("carexam", "科目四");break;
        }
        model.addAttribute("cartype", cartype);
        if(examtype.equals("random")){
            return "centerRandom";
        }else {
            return "centerExam";
        }
    }
}
